package com.uzai.console.service.wechatprivate.impl;

import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.constant.ES.ElasticsearchUtil;
import com.uzai.console.dto.wechatprivate.wxq.wxgroup.WxGroupIdDto;
import com.uzai.console.dto.wechatprivate.wxq.wxgroup.WxGroupIdListDto;
import com.uzai.console.dto.wechatprivate.wxq.wxgroup.WxGroupSendMessageBatchDto;
import com.uzai.console.dto.wechatprivate.wxq.wxgroup.WxGroupSendMessageDto;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.WxqGroupManagePlanMapper;
import com.uzai.console.mapper.WxqManagePlanMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.feign.WxContactsFeignService;
import com.uzai.console.service.rocketmq.NoticeMsgPushProducerService;
import com.uzai.console.service.wechatprivate.WxGroupService;
import com.uzai.console.service.wechatprivate.WxGroupSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 个人微信-微信群批量操作
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class WxGroupServiceSyncImpl implements WxGroupSyncService {

    Logger logger = LoggerFactory.getLogger(WxGroupServiceSyncImpl.class);

    @Autowired
    private WxGroupService wxGroupService;

    /**
     * 批量退群
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxGroupIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void quitWxGroupBatch(WxGroupIdListDto wxGroupIdListDto){
        List<String> idList = wxGroupIdListDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择群");
        }
        for (String id: idList){
            try {
                WxGroupIdDto wxGroupIdDto = new WxGroupIdDto();
                wxGroupIdDto.setMerId(wxGroupIdListDto.getMerId());
                wxGroupIdDto.setId(id);
                wxGroupService.quitWxGroup(wxGroupIdDto);
            }catch (Exception e){

            }
        }
    }

    /**
     * 批量发送消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxGroupSendMessageBatchDto
     * @return null
     */
    @Async("taskExecutor")
    public void sendMessageBatch(WxGroupSendMessageBatchDto wxGroupSendMessageBatchDto){
        List<String> idList = wxGroupSendMessageBatchDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择群");
        }

        //异步操作
        for (String id: idList){
            try {
                WxGroupSendMessageDto wxGroupSendMessageDto = new WxGroupSendMessageDto();
                wxGroupSendMessageDto.setMerId(wxGroupSendMessageBatchDto.getMerId());
                wxGroupSendMessageDto.setId(id);
                wxGroupSendMessageDto.setMessage(wxGroupSendMessageBatchDto.getMessage());
                wxGroupService.sendMessage(wxGroupSendMessageDto);
            }catch (Exception e){

            }
        }
    }

    /**
     * 批量刷新群成员列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxGroupIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void refreshGroupMemberBatch(WxGroupIdListDto wxGroupIdListDto){
        List<String> idList = wxGroupIdListDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择群");
        }
        //异步操作
        for (String id: idList){
            try {
                WxGroupIdDto wxGroupIdDto = new WxGroupIdDto();
                wxGroupIdDto.setMerId(wxGroupIdListDto.getMerId());
                wxGroupIdDto.setId(id);
                wxGroupService.refreshGroupMember(wxGroupIdDto);
            }catch (Exception e){

            }
        }
    }
}
